﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WeifenLuo.WinFormsUI.Base
{
     
    public partial class FormBaseDataGrid : FormBaseData
    {
        private bool mb_GridChanged = false;

       
        public FormBaseDataGrid()
        {
            InitializeComponent();
        }

        protected override void BasicForm_Load(object sender, EventArgs e)
        {
            SetCurrentDataGrid(mdbg_dataGrid);
            base.BasicForm_Load(sender, e);
        }

        protected override void RefreshToolbar()
        {
            base.RefreshToolbar();
            if (mRowManager == null) return;
            SetList(false);
            SetFind(true);
        }

        protected override void BaseForm_Activated(object sender, EventArgs e)
        {
            base.BaseForm_Activated(sender, e);
            if (ms_currentField != null && ms_valueCurrentField != null)
                mMainForm.stt_Bar.Text = ms_currentField + ": " + ms_valueCurrentField;
        }

        // For using set the currentField and the value current field
        public override void BaseDataForm_MouseUp(object sender, MouseEventArgs e)
        {
            if (sender.Equals(mdbg_dataGrid))
            {	// get current column when user click header.
                mi_currentCol = mdbg_dataGrid.HitTest(e.X, e.Y).ColumnIndex;
            }
        }

        public override void RefreshData()
        {
            mdbg_dataGrid.DataSource = null;
            base.RefreshData();
        }

        public override void modify()
        {
            mdbg_dataGrid.AllowUserToAddRows = false;
            mdbg_dataGrid.ReadOnly = false;
            mdbg_dataGrid.AllowUserToDeleteRows = true;
            base.modify();
        }

        public override void add()
        {
            base.add();
            // Keeping the last row
            mi_CurrentRow = mRowManager.Count - 1;
            //mdbg_dataGrid.AllowUpdate = true;
            //mdbg_dataGrid.AllowSort = false;
            //mdbg_dataGrid.EditActive = true;
            //mdbg_dataGrid.EditActive = false;
            mdbg_dataGrid.AllowUserToAddRows = true;
            mdbg_dataGrid.ReadOnly = false;
            mdbg_dataGrid.AllowUserToOrderColumns = false;
            mdbg_dataGrid.AllowUserToDeleteRows = true;
        }

        public override void save()
        {
            //mdbg_dataGrid.AllowUpdate = false;
            //mdbg_dataGrid.AllowSort = true;
            mdbg_dataGrid.AllowUserToAddRows = false;
            mdbg_dataGrid.ReadOnly = true;
            mdbg_dataGrid.AllowUserToOrderColumns = true;
            mdbg_dataGrid.AllowUserToDeleteRows = false;
            base.save();
            //mdbg_dataGrid.AllowAddNew = false;
        }

        public override void cancel()
        {
            //mdbg_dataGrid.AllowUpdate = false;
            //mdbg_dataGrid.AllowSort = true;
            mdbg_dataGrid.AllowUserToAddRows = false;
            mdbg_dataGrid.ReadOnly = true;
            mdbg_dataGrid.AllowUserToOrderColumns = true;
            mdbg_dataGrid.AllowUserToDeleteRows = false;

            base.cancel();
            //mdbg_dataGrid.AllowAddNew = false;
        }

        public override void full_mode()
        {
            mdbg_dataGrid.AllowUserToAddRows = false;
            mdbg_dataGrid.ReadOnly = true;
            mdbg_dataGrid.AllowUserToOrderColumns = true;
            mdbg_dataGrid.AllowUserToDeleteRows = false;
            
            //mdbg_dataGrid.AllowUpdate = false;
            base.full_mode();
        }

        public override void void_mode()
        {
            mdbg_dataGrid.AllowUserToAddRows = false;
            mdbg_dataGrid.ReadOnly = true;
            mdbg_dataGrid.AllowUserToOrderColumns = true;
            mdbg_dataGrid.AllowUserToDeleteRows = false;
            //mdbg_dataGrid.AllowUpdate = false;
            base.void_mode();
        }

        //protected virtual void mdbg_dataGrid_RowColChange(object sender, C1.Win.C1TrueDBGrid.RowColChangeEventArgs e)
        //{
        //    try
        //    {
        //        if (mMainForm != null && mdbg_dataGrid.DataSource != null)
        //        {
        //            ms_currentField = mdbg_dataGrid.Splits[0].DisplayColumns[mdbg_dataGrid.Col].Name;
        //            ms_valueCurrentField = mdbg_dataGrid.Splits[0].DisplayColumns[mdbg_dataGrid.Col].DataColumn.Value.ToString();
        //            mMainForm.stbUnderMouse.Text = ms_currentField + ": " + ms_valueCurrentField;
        //            ms_currentField = mdbg_dataGrid.Splits[0].DisplayColumns[mdbg_dataGrid.Col].Name;
        //            ms_valueCurrentField = mdbg_dataGrid.Splits[0].DisplayColumns[mdbg_dataGrid.Col].DataColumn.Value.ToString();
        //            mMainForm.stbUnderMouse.Text = ms_currentField + ": " + ms_valueCurrentField;
        //            mRowManager.Position = mdbg_dataGrid.Row;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Ctrl_LOG.Print("BaseDataGridForm", "In event RowCol Change.", "Error message: '" + ex.Message + "'");
        //    }
        //}

        protected virtual void mdbg_dataGrid_KeyUp(object sender, KeyEventArgs e)
        {
            // Define the ShortCut key for openning the selection form
            if (GetCurrentAction() == MODES.FULL || GetCurrentAction() == MODES.VOID) return;
            if (e.KeyCode != Keys.F9) return;
            OpenSelection(mdbg_dataGrid.Columns[mdbg_dataGrid.CurrentCell.ColumnIndex].Name); 
        }

        protected virtual void mdbg_dataGrid_Leave(object sender, System.EventArgs e)
        {
            if (mb_GridChanged)
                mb_GridChanged = false;
        }

        protected override void BasicForm_Closing(object sender, FormClosingEventArgs e)
        {
            base.BasicForm_Closing(sender, e);
            if (mb_GridChanged)
                mb_GridChanged = false;
        }

        private void mnu_Delete_Click(object sender, EventArgs e)
        {
            DeleteRowGrid();
        }

        private void mdbg_dataGrid_MouseDown(object sender, MouseEventArgs e)
        {
            if (GetCurrentAction() != MODES.ADD && GetCurrentAction() != MODES.MODIFY)
                mnu_Delete.Enabled = false;
            else
                mnu_Delete.Enabled = true;
        }

        private void mnu_Copy_Click(object sender, EventArgs e)
        {
            CopyRowsGird();
        }

        private void mnu_Paste_Click(object sender, EventArgs e)
        {
            PastRowsGird();
        }
    }
}
